package com.yidu.ladder.base.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yidu.ladder.base.domain.Role;
import com.yidu.ladder.base.domain.RoleVo;
import com.yidu.ladder.base.domain.Subset;
import com.yidu.ladder.base.service.RoleService;
import com.yidu.ladder.util.IDGenerator;

/**
 * 角色控制器
 * 
 * @author yy
 * @since 2019-12-2
 */
@Controller
@RequestMapping("/system")
public class RoleController {

	/**
	 * 关联角色业务逻辑层
	 */
	@Autowired
	private RoleService roleService;

	public RoleService getRoleService() {
		return roleService;
	}

	public void setRoleService(RoleService roleService) {
		this.roleService = roleService;
	}

	/**
	 * 管理页面
	 * 
	 * @return 用户管理页的URL
	 */
	@RequestMapping(value = "role")
	public String role() {
		return "base/role/role";
	}

	/**
	 * 给下拉框赋值
	 * 
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "subset")
	public List<Subset> selectAllSubset() {
		return roleService.selectAllSubset();
	}

	/**
	 * 检索角色
	 * 
	 * @param userVo 用户包装类
	 * @return 用户信息数据
	 */
	@ResponseBody
	@RequestMapping(value = "roles", method = RequestMethod.GET)
	public Map<String, Object> queryRole(RoleVo roleVo) {
		// 执行分页查询角色方法
		Map<String, Object> roleMap = roleService.queryRoleByCriteriaWithPage(roleVo);
		// 返回结果
		return roleMap;
	}

	/**
	 * 添加角色
	 * 
	 * @param role 角色
	 * @return 添加成功返回1，否则返回0
	 */
	@ResponseBody
	@RequestMapping(value = "role", method = RequestMethod.POST)
	public boolean addRole(Role role) {
		role.setRoleId(IDGenerator.getId());
		role.setSubsetId(role.getRoleId());
		role.setUseable("Y");
		// 执行添加角色方法
		int insert = roleService.addRole(role);
		// 返回结果
		return insert == 1 ? true : false;

	}

	/**
	 * 修改角色
	 * 
	 * @param role 角色
	 * @return 修改还原成功返回1，否则返回0
	 */
	@RequestMapping(value = "role", method = RequestMethod.PUT)
	@ResponseBody
	public boolean updateRole(Role role) {
		// 执行修改角色方法
		boolean updateRole = roleService.updateRole(role);
		// 返回结果
		return updateRole;
	}

	/**
	 * 冻结或还原角色
	 * 
	 * @param roleId  角色Id
	 * @param useable 是否可用
	 * @return 冻结或还原成功返回1，否则返回0
	 */
	@RequestMapping(value = "role", method = RequestMethod.DELETE)
	@ResponseBody
	public boolean frozenOrRecoveRole(String roleId, String useable) {
		// 执行冻结或还原角色方法
		boolean result = roleService.frozenOrRecoveRole(roleId, useable);
		// 返回结果
		return result;
	}

}
